package com.geovis.emergency.spd.controller.basic;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import com.geovis.common.core.api.Result;
import com.geovis.common.core.constant.SwaggerConstants;
import com.geovis.common.core.controller.BaseController;
import com.geovis.common.file.dto.UploadDTO;
import com.geovis.common.mybatis.page.PageParam;
import com.geovis.common.mybatis.page.PageResult;
import com.geovis.emergency.spd.biz.basic.service.IBasicMaterialService;
import com.geovis.emergency.spd.entity.basic.entity.BasicMaterial;
import com.geovis.emergency.spd.entity.basic.pojo.dto.BasicMaterialDTO;
import com.geovis.emergency.spd.entity.basic.pojo.dto.BasicMaterialQueryDTO;
import com.geovis.emergency.spd.entity.basic.pojo.dto.BasicMaterialUpdateDTO;
import com.geovis.emergency.spd.entity.basic.pojo.dto.BasicMaterialUploadDTO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.Set;

/**
 * <p>
 * 基础模块-素材管理 前端控制器
 * </p>
 *
 * @author 王响
 * @since 2022-03-31
 */
@RestController
@RequestMapping("/basic/basicMaterial")
@RequiredArgsConstructor
@Api(value = "基础模块-素材管理接口", tags = "基础模块-素材管理接口")
@Slf4j
@Validated
public class BasicMaterialController extends BaseController<IBasicMaterialService> {

    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "检查分片是否存在", notes = "检查分片是否存在")
    @GetMapping("/upload")
    public Result<UploadDTO> checkChunk(UploadDTO dto) {
        return Result.ok(dto);
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "附件上传", notes = "附件上传")
    @PostMapping(value = "/upload")
    public Result<String> upload(BasicMaterialUploadDTO uploadDTO) {
        baseService.upload(uploadDTO);
        return Result.ok();
    }

    @ApiOperationSupport(order = 3)
    @ApiOperation(value = SwaggerConstants.UPDATE_VALUE, notes = SwaggerConstants.UPDATE_NOTES)
    @PostMapping({"/update"})
    public Result<String> update(@Validated @RequestBody BasicMaterialUpdateDTO updateDTO) {
        BasicMaterial basicMaterial = baseService.getById(updateDTO.getId());
        Assert.notNull(basicMaterial,"id:{},库里没有查询要相关数据");
        BeanUtil.copyProperties(updateDTO, basicMaterial);
        baseService.updateById(basicMaterial);
        return Result.ok();
    }

    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "批量删除图片库数据", notes = "批量删除图片库数据")
    @ApiImplicitParams({@ApiImplicitParam(name = "idList", value = "删除id的list", required = true, dataType = "java.util.Set", paramType = "body")})
    @PostMapping("/removeByIdList")
    public Result<String> removeByIdList(@NotNull(message = "删除的id集合不能为空") @RequestBody(required = false) Set<String> idList) {
        baseService.removeByIdList(idList);
        return Result.ok();
    }

    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "分页获取数据", notes = "分页获取数据")
    @PostMapping("/listPage")
    public Result<PageResult<BasicMaterialDTO>> listPage(@RequestBody @Validated PageParam<BasicMaterialQueryDTO> pageParam) {
        return Result.ok(baseService.listPage(pageParam));
    }


}
